package com.kele.deadlock;

import java.util.concurrent.TimeUnit;

/**
 * @author kele
 * @date 2021/10/14 15:59
 * @description
 */
public class DeadLockTest {

    public static void main(String[] args) {
        String a="lockA";
        String b="lockB";

        new Thread(new MyThread(a,b),"t1").start();
        new Thread(new MyThread(b,a),"t2").start();
    }
}

class MyThread implements Runnable{

    private String lockA;
    private String lockB;

    public MyThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        synchronized (lockA){
            System.out.println(Thread.currentThread().getName()+" lock:"+lockA+"get : "+lockB);

            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (lockB){
                System.out.println(Thread.currentThread().getName()+" lock:"+lockB+"get : "+lockA);
            }
        }
    }
}
